<?php
namespace Website\Controller;

use \Website\Controller\BaseController;
use \Service\Service\User;
use \Service\Service\Product;
use \Service\Library\LibConfig;
use \Service\Library\LibCommon;
use \Service\Library\LibRedis;
use \Service\Library\LibAlphaId;
use \Service\Library\LibGenId;

/**
 * Index controller
 */
class Index extends BaseController
{
    public function initAction()
    {
        //echo 'This is index->init';
        
        $id = 1;
        $product_label = Product::getIns()->getProductLabel($id);

        if(!empty(LibCommon::get('type'))) {
            $this->renderJson($product_label);
        } else {
            $this->view->assign('product_label', $product_label[0]);
            $this->view->display('index');
        }

        //echo '<pre>';
        // var_dump(Product::getIns()->getProductLabel($id));
        // $id = 2;
        // var_dump(Product::getIns()->getProductLabel($id));
        // var_dump(Product::getIns()->getProductLabelById($id));
        //var_dump(LibConfig::get('local'));

        
    }

    public function someAction()
    {
        echo 'This is index->some';
    }

    /**
     * 测试 LibRedis，实际请在 Service 与 Model 层使用
     */
    public function redisAction()
    {
        // Redis
        $redis = LibRedis::getIns();
        echo '<pre>';
        var_dump($redis->set('a', 1000));
        var_dump($redis->get('a'));

        // AlphaId
        $id1 = 1000002;
        $id2 = 'IbeDa';
        $id3 = 1000003;
        $AlphaId = new LibAlphaId('JUID&@)@KS!@&#!LSA12(*', 5);
        echo '<pre>';
        echo '数字ID：', $id1, ' => 字符ID：', $AlphaId->encode($id1), "\n";
        echo '字符ID：', '3f93T', ' => 数字ID：', $AlphaId->decode('3f93T'), "\n";
        echo '数字ID：', $id3, ' => 字符ID：', $AlphaId->encode($id3), "\n";
        echo '字符ID：', $id2, ' => 数字ID：', $AlphaId->decode($id2);

        // GenId
        echo '<pre>';
        echo LibGenId::get('user'), "\n";
        echo LibGenId::get('product', rand(1, 999)), "\n";
    }
}